package controllers;

import java.util.Map;

import org.codehaus.jackson.JsonNode;

import models.logs.LoginToken;
import models.user.BaseUserInfo;
import models.user.EduUserInfo;
import models.user.LinkUserInfo;
import models.user.PersonUserInfo;
import models.user.PortraitUserInfo;
import models.user.UserInfo;
import models.user.WorkUserInfo;

import play.Logger;
import play.data.DynamicForm;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import utils.ControllerUtils;
import utils.MengException;
import utils.QiNiuUtil;
import utils.StringUtils;

import views.html.*;

public class UserProfile extends Controller {

	public static Result register() {

		UserInfo ui = (UserInfo) ControllerUtils.getParams(UserInfo.class);
		try {
			UserInfo.getInstances().register(ui);
			return redirect("/login");

		} catch (MengException e) {
			Logger.info(Json.toJson(e).toString());
			return ok(register.render(e.message));
		}
	}

	public static Result login() {

		Map<String, String> map = DynamicForm.form().bindFromRequest().data();
		String username = map.get("username");
		String password = map.get("password");
		try {
			UserInfo.getInstances().login(username, password);
			session().put("username", username);
			return redirect("/");
		} catch (MengException e) {
			Logger.info(Json.toJson(e).toString());
			return ok(login.render(e.message));
		}
	}

	public static Result logout() {
		String username = session().get("username");
		try {
			LoginToken.getInstances().destroyLoginToken(username);
			session().clear();
			return redirect("/login");
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result getUserInfoEmail(String email) {

		try {
			UserInfo ui = UserInfo.getInstances().getUserInfoEmail(email);
			return ok(Json.toJson(ui));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result getUserInfoUsername(String username) {

		try {
			UserInfo ui = UserInfo.getInstances().getUserInfoUsername(username);
			return ok(Json.toJson(ui));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result modifyUserInfo() {

		JsonNode node = request().body().asJson();
		UserInfo ui = Json.fromJson(node, UserInfo.class);

		try {
			boolean flag = UserInfo.getInstances().modifyUserInfo(ui);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result disableUserInfo(String username) {

		try {
			boolean flag = UserInfo.getInstances().disableUserInfo(username);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	public static Result activeUserInfo(String username) {

		try {
			boolean flag = UserInfo.getInstances().activeUserInfo(username);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}
	}

	// -----------BaseUserInfo ----------------

	public static Result modifyBUI() {

		BaseUserInfo bui = (BaseUserInfo) ControllerUtils.getParams(BaseUserInfo.class);
		bui.username = session().get("username");
		try {
			BaseUserInfo.getInstances().modifyBUI(bui);
			return redirect("/myprofile?tab=home");
		} catch (MengException e) {
			e.printStackTrace();
			return ok("BaseUserInfo modify 500");
		}

	}

	// -----------EduUserInfo--------------

	public static Result modifyEUI() {

		EduUserInfo eui = (EduUserInfo) ControllerUtils.getParams(EduUserInfo.class);
		eui.username = session().get("username");

		try {
			EduUserInfo.getInstances().modifyEUI(eui);
			return redirect("/myprofile?tab=edu");
		} catch (MengException e) {
			e.printStackTrace();
			return ok("EduUserInfo modify 500");
		}

	}

	// ----------------LinkUserInfo-------------------

	public static Result modifyLUI() {

		LinkUserInfo lui = (LinkUserInfo) ControllerUtils.getParams(LinkUserInfo.class);
		lui.username = session().get("username");

		try {
			LinkUserInfo.getInstances().modifyLUI(lui);
			return redirect("/myprofile?tab=link");
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}


	// ----------------PersonUerInfo-------------------

	public static Result modifyPSUI() {

		PersonUserInfo psui = (PersonUserInfo) ControllerUtils.getParams(PersonUserInfo.class);
		psui.username = session().get("username");

		try {
			PersonUserInfo.getInstances().modifyPSUI(psui);
			return redirect("/myprofile?tab=profile");
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	// ----------------PortraitUserInfo-----------------
	public static Result createPUI() {

		JsonNode json = request().body().asJson();
		PortraitUserInfo pui = Json.fromJson(json, PortraitUserInfo.class);

		try {
			boolean flag = PortraitUserInfo.getInstances().createPUI(pui);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	public static Result modifyPUI() {

		JsonNode json = request().body().asJson();
		PortraitUserInfo pui = Json.fromJson(json, PortraitUserInfo.class);

		try {
			boolean flag = PortraitUserInfo.getInstances().modifyPUI(pui);
			return ok(StringUtils.toJson("status", flag));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	public static Result getPUI(String username) {

		try {
			PortraitUserInfo pui = PortraitUserInfo.getInstances().getPUI(username);
			return ok(Json.toJson(pui));
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	// ----------------WorkUserInfo-------------------

	public static Result modifyWUI() {

		WorkUserInfo wui = (WorkUserInfo) ControllerUtils.getParams(WorkUserInfo.class);
		wui.username = session().get("username");

		try {
			WorkUserInfo.getInstances().modifyWUI(wui);
			return redirect("/myprofile?tab=work");
		} catch (MengException e) {
			e.printStackTrace();
			return ok(StringUtils.toJson(e));
		}

	}

	public static Result modifyPortrait() {

		MultipartFormData body = request().body().asMultipartFormData();
		FilePart picture = body.getFile("portrait");
		PortraitUserInfo pui = PortraitUserInfo.getInstances();
		pui.username = session().get("username");
		if (picture != null) {
//			picture.getFilename();
			// String contentType = picture.getContentType();
			pui.portrait = QiNiuUtil.uploadFile(picture.getFile());
		}
		try {
			pui.modifyPUI(pui);
			return redirect("/myprofile?tab=protrait");
		} catch (MengException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ok("upload picture 500");
		}
		
	}

}
